package com.kanshu.ksgb.fastread.doudou.module.download.activity;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.kanshu.ksgb.fastread.doudou.R;
import com.kanshu.ksgb.fastread.doudou.app.constants.ARouterConfig;
import com.kanshu.ksgb.fastread.doudou.base.baseui.BaseActivity;
import com.kanshu.ksgb.fastread.doudou.common.util.DisplayUtils;
import com.kanshu.ksgb.fastread.doudou.common.util.FileUtils;
import com.kanshu.ksgb.fastread.doudou.common.view.CustomDialog;
import com.kanshu.ksgb.fastread.doudou.module.download.fragment.DownLoadCenterFragment;
import com.kanshu.ksgb.fastread.doudou.module.download.utils.DownLoadHelper;
import com.kanshu.ksgb.fastread.doudou.common.business.dialog.CommonDialog;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by xubo on 2018/11/27
 */

@SuppressLint("SetTextI18n")
@Route(path = ARouterConfig.DOWNLOAD_DOWNLOAD_CENTER)
public class DownLoadCenterActivity extends BaseActivity {

    @BindView(R.id.has_download_size)
    TextView mTvDownloadSize;
    @BindView(R.id.remaining_storage_size)
    TextView mTvRemainingStorageSize;

    TextView mTvRight;
    DownLoadCenterFragment mFragment;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_download_center);
        ButterKnife.bind(this);
        mFragment = new DownLoadCenterFragment();
        showFragment(mFragment, "DownLoad_Center");
        initData();
    }

    private void initData(){
        setTitle("下载中心");
        mTvRight = addClearBtn(mTitle.getRightContainer(), "清空");
        mTvRight.setOnClickListener(v -> {
            CommonDialog.show(this, "是否清空全部已下载书籍？", new CustomDialog.Callback() {
                @Override
                public void onSure(Dialog dialog) {
                    if(mFragment != null){
                        mFragment.clearAllDownLoadBook();
                    }
                    DownLoadHelper.clearAllDownLoadBook();
                    dialog.dismiss();
                }

                @Override
                public void onCancel(Dialog dialog) {
                    dialog.dismiss();
                }
            }).setSureText("清空");
        });
        mTvRemainingStorageSize.setText("剩余：" + FileUtils.getReadSystem());
    }

    public void setDownloadSize(String downloadSize){
        mTvDownloadSize.setText("已用：" + downloadSize);
    }

    public int getContentId() {
        return R.id.download_content;
    }


    /**
     * 添加清空按钮
     */
    private TextView addClearBtn(FrameLayout container, CharSequence s) {
        TextView tv = new TextView(getActivity());
        tv.setText(s);
        tv.setGravity(Gravity.CENTER);
        tv.setTextColor(Color.parseColor("#333333"));
        tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, DisplayUtils.dip2px(this, 15));
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
        params.gravity = Gravity.CENTER_VERTICAL;
        container.addView(tv, params);
        container.setVisibility(View.VISIBLE);
        return tv;
    }


    public static void actionStart(Context context){
        context.startActivity(new Intent(context, DownLoadCenterActivity.class));
    }
}
